//Copyright 2008 by Wes Corp for Trafficman  All rights reserved.abstract 

package org.trafficman.trafficman_main;

import org.w3c.dom.Text;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Gallery.LayoutParams;
import org.trafficman.trafficman_main.TrafficmanScreens;

public class TrafficmanFileServerNoteEdit extends Activity {

	private EditText mTitleText;

	private Long mRowId;
	private TrafficmanFileServerNotesDbAdapter mDbHelper;
	private int int_intent_active;
	private int jv_selected_file_id;
	private TrafficmanScreens mScreens;

	private EditText m_jv_sv_password;

	private EditText m_jv_sv_user;

	private EditText m_jv_sv_url;

	private EditText m_jv_sv_title;

	private String title_text;

	private String datetime_record_created;

	private String datetime_record_updated;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Window w = getWindow();
		w.requestFeature(Window.FEATURE_LEFT_ICON);

		setContentView(R.layout.servers_note_edit);

		// get the xml layout page
		LinearLayout tmpLayout = (LinearLayout) findViewById(R.id.linearlayout_001);
		TrafficmanConstants.SCREEN_VIEWWIDTH = this.getResources().getDisplayMetrics().widthPixels;
		TrafficmanConstants.SCREEN_VIEWHEIGHT = this.getResources().getDisplayMetrics().heightPixels;
		get_JobView_Screen(); // set JobView2 background
		int resID = getResources().getIdentifier(TrafficmanConstants.SCREEN_NAME, null,
				null);
		tmpLayout.setBackground(resID);

		// mTitleText = (EditText) findViewById(R.id.title);
		m_jv_sv_user = (EditText) findViewById(R.id.jv_sv_user);
		m_jv_sv_url = (EditText) findViewById(R.id.jv_sv_url);
		m_jv_sv_title = (EditText) findViewById(R.id.jv_sv_title);
		m_jv_sv_password = (EditText) findViewById(R.id.jv_sv_password);
		
		Button confirmButton = (Button) findViewById(R.id.confirm);
		Button cancelButton = (Button) findViewById(R.id.cancel);

		mRowId = icicle != null ? icicle
				.getLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID) : null;
		if (mRowId == null) {
			Bundle extras = getIntent().getExtras();
			mRowId = extras != null ? extras
					.getLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID) : null;
		}

		populateFields();
		
		if (mRowId != null) {
			confirmButton.setText(R.string.jv_sv_save_title);
		}

		confirmButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				int_intent_active = 1;
				TrafficmanConstants.SAVE_CLICK = 1;
				setResult(RESULT_OK);
				Intent intent = new Intent();
				intent.setClass(TrafficmanFileServerNoteEdit.this,
						TrafficmanMainActivity.class);
				startActivity(intent);
				finish();
			}
		});

		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				//Constants.CANCEL_CLICK = 1;
				setResult(RESULT_CANCELED);
				Intent intent = new Intent();
				intent.setClass(TrafficmanFileServerNoteEdit.this,
						TrafficmanMainActivity.class);
				startActivity(intent);
				TrafficmanConstants.CANCEL_CLICK = 0;
				finish();
			}
		});
		//setup title bar
		title_text = getString(R.string.trafficman_file_server_add);
		this.setTitle(title_text);
		w.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.network_01);

	}

	private void get_JobView_Screen() {
		mScreens = new TrafficmanScreens(this);
	}

	private void populateFields() {
		if (mRowId != null) {
			mDbHelper = new TrafficmanFileServerNotesDbAdapter(this);
			mDbHelper.open();

			Cursor note = mDbHelper.fetchNote(mRowId);
			startManagingCursor(note);

			m_jv_sv_title.setText(note.getString(note.getColumnIndex(TrafficmanFileServerNotesDbAdapter.KEY_JV_SERVER)));
			m_jv_sv_url.setText(note.getString(note.getColumnIndex(TrafficmanFileServerNotesDbAdapter.KEY_JV_SERVER_URL)));
			m_jv_sv_user.setText(note.getString(note.getColumnIndex(TrafficmanFileServerNotesDbAdapter.KEY_JV_SERVER_USER)));
			m_jv_sv_password.setText(note.getString(note.getColumnIndex(TrafficmanFileServerNotesDbAdapter.KEY_JV_SERVER_PWD)));
			TrafficmanConstants.DATETIME_RECORD_CREATED = note
			.getString(note
					.getColumnIndex(TrafficmanFileServerNotesDbAdapter.KEY_JV_DATETIME_CREATED));

			mDbHelper.close();
			
			
		}
	}

	@Override
	protected void onFreeze(Bundle outState) {
		super.onFreeze(outState);
		outState.putLong(TrafficmanFilesNotesDbAdapter.KEY_ROWID, mRowId);
	}

	@Override
	protected void onPause() {
		if (int_intent_active == 1) {
			// do nothing
			int_intent_active = 0;
			super.onPause();
			saveState();
		} else {
			super.onPause();
			// Intent intent = new Intent();
			// intent.setClass(JobView_Files_NoteEdit.this,
			// MainMenu_Notepad.class);
			// startActivity(intent);
			finish();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		populateFields();
	}

	private void saveState() {
		if (TrafficmanConstants.SAVE_CLICK == 1) {

			String jv_server_title = m_jv_sv_title.getText().toString();
			String jv_server_url = m_jv_sv_url.getText().toString();
			String jv_server_user = m_jv_sv_user.getText().toString();
			String jv_server_pwd = m_jv_sv_password.getText().toString();

			int jv_selected_server_id =0;
			
			jv_selected_file_id = 0;

			TrafficmanDateStamp dateStamp = new TrafficmanDateStamp();
			datetime_record_updated = dateStamp.datetime_now.toString();

			if (jv_server_title.length() > 0) {
				mDbHelper = new TrafficmanFileServerNotesDbAdapter(this);
				mDbHelper.open();
				if (mRowId == null) {
					datetime_record_created = datetime_record_updated;
					long id = mDbHelper.createNote(jv_server_title, jv_server_url, jv_server_user, jv_server_pwd, jv_selected_server_id, datetime_record_created, datetime_record_updated);
					if (id > 0) {
						mRowId = id;
					}
				} else {
					datetime_record_created = TrafficmanConstants.DATETIME_RECORD_CREATED;
					mDbHelper.updateNote(mRowId, jv_server_title, jv_server_url, jv_server_user, jv_server_pwd, jv_selected_server_id, datetime_record_created, datetime_record_updated);
					TrafficmanConstants.DATETIME_RECORD_CREATED = "";
				}
				mDbHelper.close();
			}
			TrafficmanConstants.SAVE_CLICK = 0;
		} else {
			// do nothing
		}
	}

}
